
//
//  WSGoodsDetailController.m
//  FD
//
//  Created by Yasin on 2018/10/15.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  外送商品详情页
// 九宫格布局 + tableView
//加入购物车view
//这个文件内容有点多

//控制器
#import "WSGoodsDetailController.h"
//view-cell
#import "WSGoodsDetailCell.h" //高度 50
#import "WSGoodsDetailSelectCell.h" //选择xxxx cell //高度 350



#pragma mark - 表头View
@interface WSGoodsDetailHeaderView : UIView
@property (nonatomic, strong) UIImageView *iv;//图像
@property (nonatomic, strong) UILabel *nameLb;//名字 "黄焖鸡米饭"
@property (nonatomic, strong) UILabel *priceLb; // 价格 富文本了，后面改
@property (nonatomic, strong) UILabel *introduceLb;//商品介绍
@end

@implementation WSGoodsDetailHeaderView;
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}


- (void)setupView {
    self.iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    self.iv.frame = CGRectMake(0, 0, SCREEN_WIDTH, 150);
    
    CGFloat k10 = 10; //间距10
    CGFloat x1 = k10;
    CGFloat y1 = CGRectGetMaxY(self.iv.frame);
    self.nameLb = [[UILabel alloc] initWithFrame:CGRectMake(x1, y1, 200, 30)];
    self.nameLb.text = @"三个字";
    
    CGFloat x2 = CGRectGetWidth(self.nameLb.frame) + 5;
    self.priceLb = [[UILabel alloc] initWithFrame:CGRectMake(x2, y1, SCREEN_WIDTH - x2 - 10, 30)];
    self.priceLb.text = @"￥8.0起 18.0";
    
    CGFloat y3 = CGRectGetMaxY(self.nameLb.frame);
    self.introduceLb = [[UILabel alloc] initWithFrame:CGRectMake(x1, y3, SCREEN_WIDTH - 2*x1, 40)];
    self.introduceLb.text = @"djkjfkj间距就放假啊看来解放路卡积分卡阿里机房那件疯狂解放路的积分";
    self.introduceLb.textColor = [UIColor lightGrayColor];
    self.introduceLb.font = [UIFont systemFontOfSize:12.0];
    self.introduceLb.numberOfLines = 0;
    
    [self addSubview:self.iv];
    [self addSubview:self.nameLb];
    [self addSubview:self.priceLb];
    [self addSubview:self.introduceLb];
}
@end

#pragma mark - 底部 加入购物车view
@interface WSGoodsDtailAddCartBottomView:UIView
/**1.价格 富文本回头改*/
@property (nonatomic, strong) UILabel *priceLb;
/**2.减少按钮*/
@property (nonatomic, strong) UIButton *minusBtn;
/**3.增加按钮*/
@property (nonatomic, strong) UILabel *cntLb;
/**4.增加按钮*/
@property (nonatomic, strong) UIButton *addBtn;
/**5.加入购物车btn*/
@property (nonatomic, strong) UIButton *addCartBtn;
@end

@implementation WSGoodsDtailAddCartBottomView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    CGFloat y = (self.bounds.size.height - 30)*0.5;
    CGFloat h = 30;
    self.priceLb = [[UILabel alloc] initWithFrame:CGRectMake(10, y, 100, h)];
    self.priceLb.text = @"￥88.0";
    self.priceLb.font = [UIFont systemFontOfSize:14.0];
    
    CGFloat x2 = CGRectGetMaxX(self.priceLb.frame);
    self.minusBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.minusBtn setImage:[UIImage imageNamed:@"jian"] forState:(UIControlStateNormal)];
    self.minusBtn.frame = CGRectMake(x2, y, h, h);
    
    CGFloat x3 = CGRectGetMaxX(self.minusBtn.frame) + 5;
    self.cntLb = [[UILabel alloc] initWithFrame:CGRectMake(x3, y, 30, 30)];
    self.cntLb.text = @"1";
    self.cntLb.textAlignment = NSTextAlignmentCenter;
    
    CGFloat x4 = CGRectGetMaxX(self.cntLb.frame);
    self.addBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.addBtn.frame = CGRectMake(x4, y, 30, 30);
    [self.addBtn setImage:[UIImage imageNamed:@"jia"] forState:(UIControlStateNormal)];

    //添加到购物车按钮
    CGFloat x5 = (SCREEN_WIDTH - 110);
    self.addCartBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.addCartBtn.frame = CGRectMake(x5, 0, 110, self.bounds.size.height);
    NSString *addcartStr = NSLocalizedString(Home_AddCart, @"加入购物车");

    [self.addCartBtn setTitle:addcartStr forState:(UIControlStateNormal)];
    [self.addCartBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    self.addCartBtn.backgroundColor = [UIColor redColor];
    [self.addCartBtn addTarget:self action:@selector(addCartBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //添加
    [self addSubview:self.priceLb];
    [self addSubview:self.minusBtn];
    [self addSubview:self.cntLb];
    [self addSubview:self.addBtn];
    [self addSubview:self.addCartBtn];
}

//添加购物车
- (void)addCartBtnAction:(UIButton *)btn {
    NSLog(@"%s--%d 添加到购物车",__FUNCTION__,__LINE__);
}

@end

#pragma mark - 控制器
@interface WSGoodsDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tv;
/**表头*/
@property (nonatomic, strong) WSGoodsDetailHeaderView *headerView;
@property (nonatomic, strong) WSGoodsDtailAddCartBottomView *bottomView;
@end

@implementation WSGoodsDetailController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupView];
}

- (void)setupView {
    self.navigationItem.title = self.goodsTitle; //前面传递过来的字符串

    //添加tableView、底部留出 50 作为加入购物车 view
    CGRect tempRect = self.view.bounds;
    tempRect.size.height = SCREEN_HEIGHT - 50;
    self.tv.frame = tempRect;
    [self.view addSubview:self.tv];
    //添加表头
    self.tv.tableHeaderView = self.headerView;
    
    //底部view
    WSGoodsDtailAddCartBottomView *bottomView = [[WSGoodsDtailAddCartBottomView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.tv.frame), SCREEN_WIDTH, 50)];
    self.bottomView = bottomView;
    [self.view addSubview:self.bottomView];
}


- (WSGoodsDetailHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[WSGoodsDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    }
    return _headerView;
}

#pragma mark -  getter
- (UITableView *)tv {
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tv.delegate = self;
        _tv.dataSource = self;
        
        //注册
        [_tv registerNib:[UINib nibWithNibName:@"WSGoodsDetailCell" bundle:nil] forCellReuseIdentifier:@"WSGoodsDetailCell"];
        [_tv registerClass:[WSGoodsDetailSelectCell class] forCellReuseIdentifier:@"WSGoodsDetailSelectCell"];
    }
    return _tv;
}

#pragma mark - 代理方法1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        WSGoodsDetailCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"WSGoodsDetailCell"];
        return cell1;
    }else {
        WSGoodsDetailSelectCell *selectedCell = [tableView dequeueReusableCellWithIdentifier:@"WSGoodsDetailSelectCell"];
        return selectedCell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 50;
    }else {
        return 350;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

